package com.yima.baseproj.module.sys.bill;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yima.baseproj.module.app.bill.model.AppBillSelectModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BillService extends ServiceImpl<BillMapper,BillEntity> {

    /**
     * 分页+模糊查询
     * @param page
     * @param searchModel
     * @return
     */
    public Page<Map> findAllByPage(Page page, BillEntity searchModel) {
        return baseMapper.findBySearchVo(page,searchModel);
    }

    /**
     * 新增操作
     * @param billEntity
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(BillEntity billEntity) {
        baseMapper.insert(billEntity);


    }

    public BillEntity findById(int id){
        return  baseMapper.selectById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    public void edit(BillEntity billEntity){
        baseMapper.updateById(billEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(int id) {
        baseMapper.deleteById(id);
    }


    /**
     * 获取订单id
     * @param parentId
     * @param studentId
     * @param billType
     * @return
     */
    public BillEntity findBill(int parentId,int studentId,int billType){

        BillEntity bill = baseMapper.selectOne(
                new QueryWrapper<BillEntity>().eq("parent_id",parentId)
        .eq("student_id",studentId)
                        .eq("bill_type",billType)
        .eq("bill_status",0)
        .eq("del_flag",0)
        .last("limit 1"));
        return bill;
    }

    /**
     * 修改电子底片订单状态
     * @param id
     */
    public void updateBillStatus(int id){
        baseMapper.updateBillStatus(id);
    }

    /**
     * 修改实体相册订单状态
     * @param id
     */
    public void updateBillPicStatus(int id){
        baseMapper.updateBillPicStatus(id);
    }

    /**
     * 查询家长用户的订单列表
     * @param parentId
     * @return
     */
    public List<AppBillSelectModel> findAllByParentId(int parentId){
        return baseMapper.findAllByParentId(parentId);
    }

    /**
     * 查询是否解锁所有底片的订单
     * @param studentId
     * @param parentId
     * @return
     */
    public Long findBillByType(int studentId,int parentId){
        return baseMapper.selectCount(
                new QueryWrapper<BillEntity>().eq("student_id",studentId)
                .eq("parent_id",parentId)
                .eq("bill_type",0)
        );
    }

}
